package com.caowei.weicimpress.weicimpress;

import android.app.Application;
import android.content.Intent;
import android.os.Environment;
import android.os.SystemClock;
import android.test.ApplicationTestCase;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
public class ApplicationTest extends ApplicationTestCase<Application> {
    private final String TAG=ApplicationTest.class.getSimpleName();
    public ApplicationTest() {
        super(Application.class);
    }

    public void testMainActivity(){
        Intent i=new Intent();
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setClassName(MainActivity.class.getPackage().getName(),
                MainActivity.class.getName());
        getSystemContext().startActivity(i);
        SystemClock.sleep(5000000);
    }

    public void testOperationCompressActivity(){
        Intent i=new Intent();
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setClassName(OperationCompressActivity.class.getPackage().getName(),
                OperationCompressActivity.class.getName());
        getSystemContext().startActivity(i);
        SystemClock.sleep(500000);
    }

    public void testDfDu(){
        Process process=null;
        try {
            StringBuilder duCommand=new StringBuilder();
            duCommand.append("du -h --max-depth=1");
            duCommand.append(" ").append(
                    Environment.getExternalStorageDirectory()
            );
            process= Runtime.getRuntime().exec(duCommand.toString());
            InputStream in= process.getInputStream();

            int result=-1;
            byte temp[]=new byte[1024];
            StringBuilder sb=new StringBuilder();
            while ((result=in.read(temp))!=-1){
                   String str=new String(temp);
                sb.append(str);
            }
            Log.d(TAG,sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(process!=null){
                process.destroy();
            }
        }
    }

    public void testDimens(){
        float f= getSystemContext().getResources().getDimension(R.dimen._10sp);
        Log.d(TAG,"R.dimen._10sp="+f);
    }


    public void testDropLeftStr(){
        String path="///////a/b/c/////";
        String pattern="/+";
        String result= Utils.drop2SlashOfStr(path);
        Log.d(TAG,result);
    }

    public void testConcatAll(){
        String[] arr=new String[]{"1","2"};
        String[] arr1=new String[]{"1","2"};
        String[] arr2= Utils.concatAll(arr,arr1);
        Log.d(TAG, Arrays.toString(arr2));
    }

    public void testZipHandlerListFiles(){
        File f= Environment.getExternalStorageDirectory();
        File rootFile=new File(f,"Demo/Calendar_05.zip");
        ZipFileHandler zipFileHandler=new ZipFileHandler(getSystemContext());
        FileInfoBean bean=new FileInfoBean();
        bean.setRootFile(rootFile);
        MyDataList list= zipFileHandler.list(bean);
        Log.d(TAG,list.toString());
    }

    public void test1(){
        String suffixPattern="(\\.z[0-9]+)$";
        Pattern p=Pattern.compile(suffixPattern);
        Matcher m=p.matcher("444444.z11115d");
        if(m.find()){
            Log.d(TAG,true+"");
        }else{
            Log.d(TAG,false+"");
        }
    }

    public void test2(){
        Intent i=new Intent();
        i.setClassName(InputPasswdActivity.class.getPackage().getName(),
                InputPasswdActivity.class.getName());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ArrayList<FileInfoBean> beans=new ArrayList<FileInfoBean>();
        FileInfoBean bean1=new FileInfoBean();
        bean1.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean1);
        FileInfoBean bean2=new FileInfoBean();
        bean2.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean2);
        FileInfoBean bean3=new FileInfoBean();
        bean3.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean3);

        FileInfoBean bean4=new FileInfoBean();
        bean1.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean4);
        FileInfoBean bean5=new FileInfoBean();
        bean2.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean5);
        FileInfoBean bean6=new FileInfoBean();
        bean3.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean6);


        FileInfoBean bean7=new FileInfoBean();
        bean7.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean7);
        FileInfoBean bean8=new FileInfoBean();
        bean8.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean8);
        FileInfoBean bean9=new FileInfoBean();
        bean9.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean9);
        FileInfoBean bean10=new FileInfoBean();
        bean10.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean10);
        FileInfoBean bean11=new FileInfoBean();
        bean11.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean11);
        FileInfoBean bean12=new FileInfoBean();
        bean12.setFileName("caowei1/sdffsd/ddggd.zip");
        beans.add(bean12);


        i.putParcelableArrayListExtra("beans",beans);
        getSystemContext().startActivity(i);
        SystemClock.sleep(500000);
    }

    public void test3(){
        String[] str=new String[]{
                "aaa","a","bb","b","aa","bbbb"
        };

        List<String> list=Arrays.asList(str);
        Collections.sort(list,new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return lhs.compareTo(rhs);
            }
        });
        for(String str1:list){
            Log.d(TAG,str1);
        }
        Log.d(TAG,"============");
        Collections.sort(list,new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                if(rhs.startsWith(lhs)){
                    return -1;
                }else if(!rhs.startsWith(lhs)){
                    return 1;
                }else{
                    return 0;
                }
            }
        });

        for(String str1:list){
            Log.d(TAG,str1);
        }
    }

    public void test4(){
        String str="aaa";
        String str1=String.valueOf(str);
        Log.d(TAG,"==========================");
        if(str==str1){
            Log.d(TAG,"str==str1");
        }
        if(str.equals(str1)){
            Log.d(TAG,"str.equals(str1)");
        }
    }

    public void test5(){
        int w= getSystemContext().getResources().getDisplayMetrics().widthPixels;
        int h=getSystemContext().getResources().getDisplayMetrics().heightPixels;
        Log.d(TAG,"w="+w+"-----h="+h);
    }
}